<?php

namespace App\Http\Controllers;

use App\Http\Biz\SuppGoodsBiz;
use App\Http\Utils\Common\OperResult;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;

class SuppGoodsController extends Controller
{


    //根据条件获取商品列表
    public function readGoodsByCondition(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $supp_id=$req->input('supp_id','');
            $goods_name=$req->input('goods_name','');
            $group_id=$req->input('group_id','');
            $status=$req->input('status','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->readGoodsByCondition($supp_id,$goods_name,$group_id,$status);
            $group=$suppGoodsBiz->readGoodsGroupList($supp_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            Log::info(response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result,'group'=>$group]));
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result,'group'=>$group]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //获取商品分组列表数据
    public function readGoodsGroupList(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $supp_id=$req->input('supp_id','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->readGoodsGroupList($supp_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //添加商品
    public function addGoods(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('goods')||!$req->has('supp_id')||!$req->has('account_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $goods=$req->input('goods','');
            $supp_id=$req->input('supp_id');
            $account_id=$req->input('account_id','');
            $goods['supp_id']=$supp_id;
            $goods['creator_ownto_id']=$supp_id;
            $goods['updator_account_id']=$account_id;
            $goods['account_types']=1;//供应商
            $goods['creator_account_id']=$account_id;
            $goods['goods_type']=2;
            $group=explode(',',$req->input('group',''));
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->addGoods($goods,$group,$supp_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_CREATE_OK,
                'msg'=>OperResult::CODE_CREATE_OK_DESC,'data'=>$result]);
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //更新商品信息
    public function updateGoodsMsg(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('goods')||!$req->has('updator_account_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $goods=$req->input('goods','');
            $goods['updator_account_id']=$req->input('updator_account_id','');
            $group=explode(',',$req->input('group',''));
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->updateGoodsMsg($goods,$group);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            if($result['code']==1){
                return response()->json(['code'=>OperResult::CODE_UPDATE_OK,
                    'msg'=>OperResult::CODE_UPDATE_OK_DESC]);
            }else{
                return response()->json(['code'=>OperResult::CODE_UPDATE_FAIL,
                    'msg'=>OperResult::CODE_UPDATE_FAIL_DESC]);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //查看商品详情
    public function readGoodsById(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('id')||!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $id=$req->input('id','');
            $supp_id=$req->input('supp_id','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->readGoodsById($id,$supp_id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result['res'],'dict'=>$result['dict'],'group'=>$result['group']]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //删除商品
    public function delectGoodsByIds(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('del_ids')||!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $del_ids=$req->input('del_ids');
            $supp_id=$req->input('supp_id','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->delectGoodsByIds($del_ids,$supp_id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            if($result['code']==1){
                return response()->json(['code'=>OperResult::CODE_DELETE_OK,
                    'msg'=>OperResult::CODE_DELETE_OK_DESC]);
            }else{
                return response()->json(['code'=>OperResult::CODE_DELETE_FAIL,
                    'msg'=>OperResult::CODE_DELETE_FAIL_DESC,'data'=>$result['data']]);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }




    //商品上架或下架
    public function goodsOnlineOrDownline(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if(!$req->has('ids')||!$req->has('supp_id')||!$req->has('type')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $ids=$req->input('ids');
            $supp_id=$req->input('supp_id','');
            $type=$req->input('type','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->goodsOnlineOrDownline($supp_id,$ids,$type);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            if($result['code']==1){
                return response()->json(['code'=>OperResult::CODE_UPDATE_OK,
                    'msg'=>OperResult::CODE_UPDATE_OK_DESC]);
            }else{
                return response()->json(['code'=>OperResult::CODE_UPDATE_FAIL,
                    'msg'=>OperResult::CODE_UPDATE_FAIL_DESC,'data'=>$result['data']]);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //添加商品规格
    public function addSpec(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('spec')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $spec=$req->input('spec','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->addSpec($spec);

            if($result['code']==-1){
                Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_CREATE_OK,
                'msg'=>OperResult::CODE_CREATE_OK_DESC]);
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //更新商品规格
    public function updateGoodsSpecMsg(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('spec')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            
            $spec=$req->input('spec','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->updateGoodsSpecMsg($spec);
            if($result['code']==-1){
                Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_UPDATE_OK,
                'msg'=>OperResult::CODE_UPDATE_OK_DESC]);
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //查看商品规格详情
    public function readSpecById(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('id')||!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $id=$req->input('id','');
            $supp_id=$req->input('supp_id','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->readSpecById($id,$supp_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            Log::info(response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result['res'],'dict'=>$result['dict']]));
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result['res'],'dict'=>$result['dict']]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //删除商品规格
    public function deleteGoodsSpecMsg(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('del_ids')||!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $del_ids=$req->input('del_ids');
            $supp_id=$req->input('supp_id','');
            $goods_id=$req->input('goods_id','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->deleteGoodsSpecMsg($del_ids,$supp_id,$goods_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            if($result['code']==1){
                return response()->json(['code'=>OperResult::CODE_DELETE_OK,
                    'msg'=>OperResult::CODE_DELETE_OK_DESC]);
            }else{
                return response()->json(['code'=>OperResult::CODE_DELETE_FAIL,
                    'msg'=>OperResult::CODE_DELETE_FAIL_DESC,'data'=>$result['data']]);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //查看商品所有的规格
    public function readAllGoodsSpec(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('id')||!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $id=$req->input('id','');
            $supp_id=$req->input('supp_id','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->readAllGoodsSpec($id,$supp_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result['res'],'dict'=>$result['dict']]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //商品规格上架或下架
    public function specOnlineOrDownline(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('ids')||!$req->has('supp_id')||!$req->has('type')||!$req->has('goods_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $ids=$req->input('ids');
            $supp_id=$req->input('supp_id','');
            $type=$req->input('type','');
            $goods_id=$req->input('goods_id','');
            $suppGoodsBiz=new SuppGoodsBiz();
            $result=$suppGoodsBiz->specOnlineOrDownline($supp_id,$ids,$type,$goods_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            if($result['code']==1){
                return response()->json(['code'=>OperResult::CODE_UPDATE_OK,
                    'msg'=>OperResult::CODE_UPDATE_OK_DESC]);
            }else{
                return response()->json(['code'=>OperResult::CODE_UPDATE_FAIL,
                    'msg'=>OperResult::CODE_UPDATE_FAIL_DESC]);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }
}
